<template>
  <view class="coupon-management">
    <!-- 顶部导航栏 -->
    <view class="nav-bar">
      <view class="nav-background"></view>
      <view class="nav-content">
        <!-- 新增优惠券按钮 -->
        <view class="add-coupon-section">
          <button class="add-coupon-btn" @click="addCoupon">
            <uni-icons type="plus" size="20" color="#3183FB" />
            <text class="btn-text">新增优惠券</text>
          </button>
        </view>
      </view>
    </view>

    <!-- 标签页和优惠券列表合并模块 -->
    <view class="tab-coupon-module">
      <!-- 标签页 -->
      <view class="tab-section">
        <view class="tab-container">
          <view 
            class="tab-item" 
            :class="{ 'tab-active': activeTab === 'active' }"
            @click="switchTab('active')"
          >
            <text class="tab-text">进行中</text>
          </view>
          <view 
            class="tab-item" 
            :class="{ 'tab-active': activeTab === 'used' }"
            @click="switchTab('used')"
          >
            <text class="tab-text">已结束</text>
          </view>
        </view>
      </view>

      <!-- 优惠券列表 -->
      <scroll-view class="coupon-list" scroll-y enhanced :show-scrollbar="false">
      <!-- 进行中的优惠券 -->
      <view v-if="activeTab === 'active'" class="coupon-container">
        <view 
          class="coupon-card" 
          v-for="(coupon, index) in activeCoupons" 
          :key="index"
          @click="viewCouponDetail(coupon)"
        >
          <view class="coupon-left">
            <view class="coupon-condition">{{ coupon.condition }}</view>
            <view class="coupon-amount">¥{{ coupon.amount }}</view>
          </view>
          <view class="coupon-right">
            <view class="coupon-name">优惠券名称: {{ coupon.name }}</view>
            <view class="coupon-validity">有效期: {{ coupon.validity }}</view>
            <view class="coupon-limit">{{ coupon.limit }}</view>
          </view>
        </view>
        
        <!-- 空状态 -->
        <view v-if="activeCoupons.length === 0" class="empty-state">
          <view class="empty-icon">
            <uni-icons type="gift" size="60" color="#E0E0E0" />
          </view>
          <text class="empty-text">暂无进行中的优惠券</text>
          <text class="empty-subtitle">点击上方按钮添加新的优惠券</text>
        </view>
      </view>

      <!-- 已结束的优惠券 -->
      <view v-if="activeTab === 'used'" class="coupon-container">
        <view 
          class="coupon-card used" 
          v-for="(coupon, index) in usedCoupons" 
          :key="index"
          @click="viewCouponDetail(coupon)"
        >
          <view class="coupon-left">
            <view class="coupon-condition">{{ coupon.condition }}</view>
            <view class="coupon-amount">¥{{ coupon.amount }}</view>
          </view>
          <view class="coupon-right">
            <view class="coupon-name">优惠券名称: {{ coupon.name }}</view>
            <view class="coupon-validity">有效期: {{ coupon.validity }}</view>
            <view class="coupon-limit">{{ coupon.limit }}</view>
            <view class="used-overlay">已结束</view>
          </view>
        </view>
        
        <!-- 空状态 -->
        <view v-if="usedCoupons.length === 0" class="empty-state">
          <view class="empty-icon">
            <uni-icons type="checkmarkempty" size="60" color="#E0E0E0" />
          </view>
          <text class="empty-text">暂无已结束的优惠券</text>
          <text class="empty-subtitle">已结束的优惠券将在这里显示</text>
        </view>
      </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      activeTab: 'active', // 'active' 或 'used'
      activeCoupons: [
        {
          condition: '满200元可用',
          amount: '20',
          name: '新用户专享体验券',
          validity: '2020.3.18-2023.4.18',
          limit: '每人限领一张'
        },
        {
          condition: '满100元可用',
          amount: '10',
          name: '春季特惠券',
          validity: '2023.4.1-2023.6.30',
          limit: '每人限领三张'
        },
        {
          condition: '满500元可用',
          amount: '50',
          name: '会员专享券',
          validity: '2023.3.1-2023.12.31',
          limit: '会员专享'
        },
        {
          condition: '满300元可用',
          amount: '30',
          name: '周末特惠券',
          validity: '2023.5.1-2023.8.31',
          limit: '每人限领两张'
        },
        {
          condition: '满150元可用',
          amount: '15',
          name: '生日专享券',
          validity: '2023.1.1-2023.12.31',
          limit: '生日当天可用'
        }
      ],
      usedCoupons: [
        {
          condition: '满200元可用',
          amount: '20',
          name: '新用户专享体验券',
          validity: '2020.3.18-2020.4.18',
          limit: '每人限领一张'
        },
        {
          condition: '满200元可用',
          amount: '20',
          name: '新用户专享体验券',
          validity: '2020.3.18-2020.4.18',
          limit: '每人限领一张'
        }
      ]
    };
  },
  methods: {
    addCoupon() {
      // 跳转到发放优惠券页面
      uni.navigateTo({
        url: '/pages/coupon-management/coupondetails/coupondetails'
      });
    },
    switchTab(tab) {
      this.activeTab = tab;
    },
    viewCouponDetail(coupon) {
      console.log('查看优惠券详情:', coupon);
      uni.showToast({
        title: '查看优惠券详情',
        icon: 'none'
      });
    }
  }
};
</script>

<style scoped>
.coupon-management {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  position: relative;
  display: flex;
  flex-direction: column;
}

/* 导航栏样式 */
.nav-bar {
  position: relative;
  z-index: 10;
  height: 140px;
  padding-top: 30px;
  overflow: hidden;
}

.nav-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #1F73F1;
  box-shadow: 0 8px 32px rgba(31, 115, 241, 0.3);
}

.nav-content {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  height: 110px;
  padding: 0 20px 20px;
}


/* 新增优惠券按钮区域 */
.add-coupon-section {
  margin-top: -20px;
  padding: 0;
  position: relative;
  z-index: 5;
 
}

.add-coupon-btn {
  width: 100%;
  background: #fff;
  border: none;
  border-radius: 16px;
  padding: 10px 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.add-coupon-btn:active {
  transform: scale(0.98);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
}

.btn-text {
  font-size: 16px;
  color: #3388FF;
  font-weight: 600;
}

/* 标签页和优惠券列表合并模块 - 整体绝对定位 */
.tab-coupon-module {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 16px;
  margin: 0 20px 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  position: absolute;
  top: 100px;
  left: 0;
  right: 0;
  z-index: 10;
  min-height: calc(100vh - 100px);
}

/* 标签页样式 */
.tab-section {
  padding: 20px 20px 0;
  position: relative;
  z-index: 5;
  background: transparent;
}

.tab-container {
  display: flex;
  background: #f5f5f5;
  border-radius: 12px;
  padding: 4px;
  position: relative;
  z-index: 1000;
}

.tab-item {
  flex: 1;
  padding: 12px 20px;
  border-radius: 12px;
  text-align: center;
  transition: all 0.3s ease;
  cursor: pointer;
  position: relative;
  z-index: 1001;
}

.tab-active {
  background: linear-gradient(135deg, #3183FB 0%, #3183FB 100%);
  box-shadow: 0 4px 12px rgba(49, 131, 251, 0.3);
}

.tab-text {
  font-size: 14px;
  font-weight: 500;
  color: #666;
  transition: color 0.3s ease;
}

.tab-active .tab-text {
  color: #fff;
}

/* 优惠券列表 */
.coupon-list {
  flex: 1;
  padding: 0 20px 20px;
  position: relative;
  z-index: 5;
  height: calc(100vh - 180px);
  overflow-y: auto;
}

.coupon-container {
  display: flex;
  flex-direction: column;
  gap: 0;
  padding-bottom: 20px;
}

/* 优惠券卡片 */
.coupon-card {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  border: 1px solid #f0f0f0;
  display: flex;
  transition: all 0.3s ease;
  position: relative;
  margin-bottom: 12px;
}

.coupon-card:active {
  transform: translateY(-2px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.12);
}

.coupon-card.used {
  opacity: 0.7;
}

/* 优惠券左侧 */
.coupon-left {
  width: 100px;
  background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 16px 12px;
  position: relative;
  min-height: 80px;
}

.coupon-left::after {
  content: '';
  position: absolute;
  right: -8px;
  top: 50%;
  transform: translateY(-50%);
  width: 16px;
  height: 16px;
  background: #f5f7fa;
  border-radius: 50%;
}

.coupon-condition {
  font-size: 11px;
  color: #fff;
  font-weight: 500;
  margin-bottom: 6px;
  text-align: center;
  line-height: 1.2;
}

.coupon-amount {
  font-size: 20px;
  color: #fff;
  font-weight: 700;
  text-align: center;
  line-height: 1;
}

/* 优惠券右侧 */
.coupon-right {
  flex: 1;
  padding: 16px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: relative;
  background: #fafafa;
}

.coupon-name {
  font-size: 13px;
  color: #333;
  font-weight: 500;
  margin-bottom: 6px;
  line-height: 1.3;
}

.coupon-validity {
  font-size: 13px;
  color: #666;
  margin-bottom: 6px;
  line-height: 1.3;
}

.coupon-limit {
  font-size: 13px;
  color: #999;
  line-height: 1.3;
}

/* 已结束覆盖层 */
.used-overlay {
  position: absolute;
  top: 50%;
  right: 20px;
  transform: translateY(-50%) rotate(-15deg);
  background: rgba(0, 0, 0, 0.1);
  color: #999;
  font-size: 16px;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: 8px;
  backdrop-filter: blur(10px);
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  text-align: center;
}

.empty-icon {
  margin-bottom: 20px;
  opacity: 0.6;
}

.empty-text {
  font-size: 18px;
  color: #666;
  font-weight: 500;
  margin-bottom: 8px;
}

.empty-subtitle {
  font-size: 14px;
  color: #999;
}
</style>
